package cn.yll.web.controller;

import cn.yll.common.exception.BsErrEnum;
import cn.yll.common.exception.BsException;
import cn.yll.common.module.RestModel;
import cn.yll.common.utils.RedisUtil;
import cn.yll.api.util.RestUtil;
import cn.yll.api.util.VerifyCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * 测试接口
 * @author yangll
 */
@Slf4j
@RefreshScope
@RestController
@Api(tags = "网站用相关接口")
@RequestMapping("index")
public class IndexController extends BaseController {
    @Autowired
    private RedisUtil redisUtil;

    @Value("${sysConfig}")
    private String sysConfig;

    @GetMapping("config/sysConfig")
    @ApiOperation(value = "获取动态配置", notes = "测试nacos动态配置")
    public RestModel<String> getSysConfig() {
        return RestUtil.sucess(sysConfig);
    }

    @GetMapping("verify/imgCode")
    @ApiOperation(value = "获取图形验证码接口", notes = "key必须传入，建议使用系统时间戳，校验接口也得传入相同的key")
    @ApiImplicitParam(name = "key", value = "图形验证码校验码", dataType = "string", paramType = "query", required = true)
    public void imgVerifyCode(String key, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(key)) {
            throw new BsException(BsErrEnum.PARAM_EMPTY);
        }
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        try {
            //输出验证码图片
            String randCode = VerifyCodeUtil.getRandomCode(response);
            redisUtil.set(key, randCode, 5, TimeUnit.MINUTES);
        } catch (Exception e) {
            log.error("获取验证码异常：", e);
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
    }

    @GetMapping("verify/imgCode/valid")
    @ApiOperation(value = "校验图形验证码接口", notes = "key和code必须传入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "获取图形验证码时的校验码", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "图形验证码", dataType = "string", paramType = "query", required = true)
    })
    public RestModel<?> imgVerifyCodeValid(String key, String code) {
        if (StringUtils.isAnyBlank(key, code)) {
            throw new BsException(BsErrEnum.PARAM_EMPTY);
        }
        String v = redisUtil.getV(key);
        if (StringUtils.isBlank(v)) {
            return RestUtil.error(BsErrEnum.IMGCODE_EXPIRED);
        }
        if (!v.equalsIgnoreCase(code)) {
            return RestUtil.error(BsErrEnum.IMGCODE_ERROR);
        }
        redisUtil.delete(key);
        return RestUtil.sucess();
    }

}
